"use strict";
const common_vendor = require("./vendor.js");
const SEVERURL = "https://mock.apifox.cn/m1/2680723-0-default";
const WITHEURL = ["/visitor/auth/login"];
function service(options = {}) {
  console.log("请求" + options.url + "中...");
  let urlIsNoToken = WITHEURL.indexOf(options.url) !== -1;
  let user = common_vendor.index.getStorageSync("user");
  options.url = `${SEVERURL}${options.url}`;
  options.header = { "Content-Type": "application/json" };
  if (!urlIsNoToken) {
    if (!user && !user.token) {
      return new Promise((resolved, rejected) => {
        common_vendor.index.reLaunch({ url: "/pages/login/login" }).then((res) => {
          common_vendor.index.showToast({ title: "未登录，请登录后访问", icon: "none" });
          rejected({ "err_msg": "用户信息不存在，跳转到登录页面" });
        });
      });
    }
    options.header.Authorization = `Bearer ${user.token}`;
  }
  return new Promise((resolved, rejected) => {
    options.fail = (err) => {
      rejected(err);
    };
    options.success = (res) => {
      if (res.statusCode == 200) {
        resolved(res.data);
      } else {
        rejected(res);
      }
    };
    common_vendor.index.request(options);
  });
}
exports.service = service;
